home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / applet / communication / example / QuoteServerThread.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.3 KB  |  98 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.io.*;
  18. import java.net.*;
  19. import java.util.*;
  20.  
  21. class QuoteServerThread extends Thread {
  22.     private DatagramSocket socket = null;
  23.     private DataInputStream qfs = null;
  24.  
  25.     QuoteServerThread() {
  26.         super("QuoteServer");
  27.         try {
  28.             socket = new DatagramSocket();
  29.             System.out.println("QuoteServer listening on port: " + socket.getLocalPort());
  30.         } catch (java.io.IOException e) {
  31.             System.err.println("Could not create datagram socket.");
  32.         }
  33.         this.openInputFile();
  34.     }
  35.  
  36.     public void run() {
  37.         if (socket == null)
  38.             return;
  39.  
  40.         while (true) {
  41.             try {
  42.                 byte[] buf = new byte[256];
  43.                 DatagramPacket packet;
  44.                 InetAddress address;
  45.                 int port;
  46.                 String dString = null;
  47.  
  48.                     // receive request
  49.                 packet = new DatagramPacket(buf, 256);
  50.                 socket.receive(packet);
  51.                 address = packet.getAddress();
  52.                 port = packet.getPort();
  53.  
  54.                     // send response
  55.                 if (qfs == null)
  56.                     dString = new Date().toString();
  57.                 else
  58.                     dString = getNextQuote();
  59.                 dString.getBytes(0, dString.length(), buf, 0);
  60.                 packet = new DatagramPacket(buf, buf.length, address, port);
  61.                 socket.send(packet);
  62.             } catch (IOException e) {
  63.                 System.err.println("IOException:  " + e);
  64.                 e.printStackTrace();
  65.             }
  66.         }
  67.     }
  68.     protected void finalize() {
  69.         if (socket != null) {
  70.                 socket.close();
  71.                 socket = null;
  72.                 System.out.println("Closing datagram socket.");
  73.         }
  74.     }
  75.  
  76.     private void openInputFile() {
  77.         try {
  78.             qfs = new DataInputStream(new FileInputStream("one-liners.txt"));
  79.         } catch (java.io.FileNotFoundException e) {
  80.             System.err.println("Could not open quote file. Serving time instead.");
  81.         }
  82.     }
  83.     private String getNextQuote() {
  84.         String returnValue = null;
  85.         try {
  86.             if ((returnValue = qfs.readLine()) == null) {
  87.                 qfs.close();
  88.                 this.openInputFile();
  89.                 returnValue = qfs.readLine();    // we know the file has at least one input line!
  90.             }
  91.         } catch (IOException e) {
  92.             returnValue = "IOException occurred in server.";
  93.         }
  94.         return returnValue;
  95.     }
  96.  
  97. }
  98.